package logic;

import java.util.TreeSet;

import data.RatingTuple;
import data.SystemData;
import data.UserTuple;
import exceptions.MissingMandantoryFieldException;
import exceptions.UserAlreadyExistException;
import exceptions.WrongPasswordException;

public class UserHandler {

	public UserHandler(){

	}

	public boolean login(String userName, String password){
		UserTuple userT= SystemData.getInstance().getUser(userName);
		if (userT == null){
				ToGuiFacade.getInstance().showLoginErrorScreen("Wrong username");
				return false;
		}
		if (userT.isPassword(password)){			
			Logger.getInstance().userLogIn(userT);
			ToGuiFacade.getInstance().showUserScreen(userName , userT.isAdmin());
			return true;
		}
		ToGuiFacade.getInstance().showLoginErrorScreen("Wrong username or password");
		return false;
	}

	public boolean logout(/*String userName*/) {
		//UserTuple userT= SystemData.getInstance().getUser(userName);
		//if (userT == null){
			//TODO return false; //need to figure out what to do here, why should the system care who is logged in? and how am i supposed to know who is the active user?
	//	}else {
//			userT.set_loggedIn(false);
		//} //TODO for now we dont care who logged out
		//Logger.getInstance().userLogOut(userT);
		ToGuiFacade.getInstance().showLoginScreen();
		return true;
	}

	public void registerUser(String userName, String password1,
		String password2, String day, String month, String year, String sex) throws WrongPasswordException, MissingMandantoryFieldException, UserAlreadyExistException{
		if (!(password2.equals(password1))){
			ToGuiFacade.getInstance().passwordsDontMatch();
		}
		else if (userName.equals("")||(day.equals(""))||(year.equals(""))||(month.equals(""))){
			ToGuiFacade.getInstance().missingMandantoryField();
		}
		else{
			
			if (SystemData.getInstance().getUser(userName)!=null){
				ToGuiFacade.getInstance().userAlreadyExists();
			}
			else{
				UserTuple user = SystemData.getInstance().createUser(userName, password1, sex,day+"/" + month +"/"+ year, "user");
			//	user.encryptPassword();
			//	SystemData.getInstance().addUser(user);
				ToGuiFacade.getInstance().showUserScreen(user.get_name(), user.isAdmin());
			}
		}

	}
	public void changeDetails(String name, String oldpass, String pass1, String pass2,
			String day, String month, String year, boolean b) {
		UserTuple user = SystemData.getInstance().getUser(name);
		if (!user.isPassword(oldpass)){
			ToGuiFacade.getInstance().wrongPasswordChange();
		}else if((!("".equals(pass1)) || !("".equals(pass2)))  && !pass1.equals(pass2)){
			ToGuiFacade.getInstance().passwordsDontMatchOnUserChange();
		}else{
			
		//	user.set_bdate(day+"/" + month +"/"+ year);
		//	user.set_sex((sex.equals("Male")) ? false : true);
			SystemData.getInstance().editUser(name,day+"/" + month +"/"+ year, b);
			if (!pass1.equals("")){
				SystemData.getInstance().changeUserPassword(name, pass1);
				//user.set_password(pass1);
			}
			
			
			ToGuiFacade.getInstance().showUserScreenAfterChangeDetails(name, user.isAdmin());
		}
	}

	public void getMovies(String user, int begin) {
		ToGuiFacade.getInstance().showRecMovies(SystemData.getInstance().getUser(user).getRecs(begin),begin);

	}

	public void hasSeenMovie(String movieName, String userName){
		SystemData sd = SystemData.getInstance();
		if (sd.getRating(userName, movieName)==null)
			
			ToGuiFacade.getInstance().setRemoveRatingButton(false);
		else ToGuiFacade.getInstance().setRemoveRatingButton(true);
	}

	public int getNumRating(String user) {
		SystemData sysdata = SystemData.getInstance();
		return sysdata.getRatingsByUser(user).size();
	}

	public TreeSet<RatingTuple> getUserRatings(String user) {
		return SystemData.getInstance().getUser(user).getRatings();
	}
	

	public String[] haveSeenRating(String user, String movie) {
		RatingTuple rating = SystemData.getInstance().getRating(user, movie);
		String ans[] = {null, null};
		if (rating != null){
			ans[0] = ""+rating.get_rating()+"";
			ans[1] = rating.get_description();
		}
		return ans;
	}
}
